package com.qf.purchasing.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.purchasing.entity.Detail;
import com.qf.purchasing.mapper.DetailMapper;
import com.qf.purchasing.qo.DetailQO;
import com.qf.purchasing.service.DetailService;
import com.qf.purchasing.utils.CommonBeanUtils;
import com.qf.purchasing.vo.DetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class DetailServiceImpl implements DetailService {
    @Resource
    private DetailMapper detailMapper;

    @Override
    public PageInfo<DetailVO> query(int page, int size, int sid) {
        PageInfo<Detail> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(
                () -> detailMapper.selectDetailsBySid(sid)
        );
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }

        return CommonBeanUtils.copyPageInfo(pageInfo, new PageInfo<>(), DetailVO::new);

    }

    @Override
    public RespResult<String> addDetailService(DetailQO detailQO) {
        //查询此条明细是否存在，根据id判断
        Detail detailResult = detailMapper.selectById(detailQO.getDetailId());
        if (ObjectUtils.isEmpty(detailResult)) {
            //不存在，插入
            Detail detail = new Detail();
            BeanUtils.copyProperties(detailQO, detail);
            int count = detailMapper.insert(detail);
            if (count < 1) {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }
            return RespResult.success("添加成功！");
        } else {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public RespResult<String> deleteDetailByIds(int[] detailIds) {
        int count = detailMapper.updateStatusById(detailIds);
        if (count < 1) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
        return RespResult.success("记录删除成功！");
    }

    @Override
    public RespResult<String> modify(DetailQO detailQO) {

        Detail detail = new Detail();
        BeanUtils.copyProperties(detailQO, detail);
        int count = detailMapper.updateById(detail);
        if (count < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("记录更新成功！");
    }

    @Override
    public RespResult<DetailVO> selectDetailById(int detailId) {
        Detail detail = detailMapper.selectById(detailId);
        if (!ObjectUtils.isEmpty(detail)) {
            DetailVO detailVO = new DetailVO();
            BeanUtils.copyProperties(detail, detailVO);
            return RespResult.success(ResultCode.SUCCESS, detailVO);
        } else {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }
}
